window.onload=function() {
    // 跳转到首页
    var all_main = document.querySelector(".nav-top-box-1").children[0]
    all_main.addEventListener("click", function () {
        window.location.href = "/";
    });
    // 跳转到主页
    var exit = document.querySelector(".exit")
    exit.addEventListener("click",function(){
        $.ajax({
            type: "post",
            url: "/User/loginOut",
            // data: {
            // },
            success: function (data) {
                // alert("退出")
                window.location.href="/";

            },error: function () {
                console.log("調取接口失敗")
            }
        })
    });
    // 跳转到个人中心
    var userInterface=document.querySelector(".nav-top-box-1").children[2];
    console.log(userInterface);
    userInterface.addEventListener("click",function(){
        window.location.href = "/User/userInterface";
    })
    // 获取userid
    var id=document.querySelector(".id");
    // 用户喜欢的数据渲染到页面上
    var main=document.querySelector(".main");
    function fenYe(x) {
        main.innerHTML="";
        $.ajax({
            type: "post",
            url: "/Like/selectAllLike",
            data: {
                userId: x
            },
            success: function (data) {
                for (let i=0;i<data.length;i++){
                    var x="";
                    var goodsName;
                    if(data[i].goodsName.length>10){
                        for (var j=0;j<=10;j++) {
                            x = x + data[i].goodsName[j];
                            goodsName=x+"...";
                        }
                    }else{
                        x=data[i].goodsName;
                        goodsName=x;
                    }
                    let goods=` <div class="goods">
                                    <p class="goodsid" style="display: none">${localStorage.getItem("goodsid")}</p>
                                    <img src="${data[i].goodsImg}">
                                    <button class="goods-del">x</button>
                                    <h3>${goodsName}</h3>
                                    <p class="shop">${data[i].shopName}</p>
                                    <p class="price">￥${data[i].goodsPrice}</p>
                                </div>`;
                    main.insertAdjacentHTML("beforeend",goods);
                }
                // 删除单个订单(已支付)
                var goods_del=document.querySelectorAll(".goods-del");
                for (let i = 0; i <goods_del.length; i++) {
                    goods_del[i].addEventListener("click", function (e) {
                        e.stopPropagation();
                        if (confirm("你确定要删除此喜欢吗?")) {
                            del_only(data[i].likeId);
                        }
                    })
                }
                // 跳转到商品详情
                let goods=document.querySelectorAll(".goods");
                for (let i = 0; i <goods.length; i++) {
                    goods[i].addEventListener("click", function () {
                        xiangqing(data[i].goodsId);
                        localStorage.setItem("name",data[i].goodsId);
                        window.location.href="/goodsDetail";
                    })
                }
            }
        })
    }
    fenYe(id.innerHTML);
    // 删除全部喜欢
    function del(x) {
        var delete_1 = document.querySelector(".all-delete")
        delete_1.addEventListener("click", function () {
            if (confirm("你确定要删除全部喜欢吗?")){
                $.ajax({
                    type: "post",
                    url: "/Like/deleteAllLike",
                    data: {
                        userId: x
                    },
                    success: function (page) {
                        alert("删除成功");
                        fenYe(id.innerHTML);
                    }
                })
            }
        })
    }
    del(id.innerHTML);
    // 删除单个
    function del_only(x) {
        $.ajax({
            type: "post",
            url: "/Like/deleteLike",
            data: {
                likeId:x
            },
            success: function (page) {
                alert("删除成功");
                fenYe(id.innerHTML);
            }
        })
    }
    // // 查询商品详情页
    function xiangqing(x) {
        $.ajax({
            type: "get",
            url: "/client/findGoodsBygoodsId",
            data: {
                goodsId: x
            },
            success: function (page) {
                console.log(page);

            }
        })
    }

}